//
//  TimerCloseViewController.m
//  ;
//
//  Created by 1564335 on 15-6-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TimerCloseViewController.h"

@interface TimerCloseViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property(nonatomic, strong) UIPickerView *timerPickerView;

@property(nonatomic, strong) NSTimer *timer;

@end

@implementation TimerCloseViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
    
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, _HEIGHT)];
    [self.view addSubview:backgroundImageView];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    backgroundView.frame = CGRectMake(0, 0, _WIDTH, _HEIGHT);
    [self.view addSubview:backgroundView];
    [backgroundImageView setImage:[UIImage imageNamed:@"bgImage2"]];
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame = CGRectMake(10, 20, 30, 30);
    [buttonBack setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(backFromSelf) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBack];
    
    _timerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 100)];
    [self.view addSubview:_timerPickerView];
    _timerPickerView.delegate = self;
    _timerPickerView.dataSource = self;
//    _timerPickerView.center = self.view.center;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    button.frame = CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 , 80, 40);
    [button addTarget:self action:@selector(getTimer:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"确  定" forState:UIControlStateNormal];
}

- (void)getTimer:(UIButton *)button
{
    [_timer invalidate];
    NSInteger totalTime = [_timerPickerView selectedRowInComponent:0] * 60 * 60 + [_timerPickerView selectedRowInComponent:1] * 60;
    if (totalTime > 0) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:totalTime target:self selector:@selector(stopPlayer) userInfo:nil repeats:NO];
    }
}

- (void)stopPlayer
{
    [[MusicPlayer1 shareInstance].moviePlayer pause];
    [_timer invalidate];
}

- (void)backFromSelf
{
    [self dismissViewControllerAnimated:YES completion:^{
        [[CDSideBarController sharedInstance] dismissMenu];
    }];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return 24;
    }
    return 60;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return [NSString stringWithFormat:@"%ld 时", row];
    }
    return [NSString stringWithFormat:@"%ld 分", row];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
